package com.xjiye_test.payServer.dao;

import com.xiaoleilu.hutool.db.Entity;
import com.xiaoleilu.hutool.db.Page;
import com.xiaoleilu.hutool.db.PageResult;
import com.xiaoleilu.hutool.db.SqlRunner;
import com.xiaoleilu.hutool.db.ds.DSFactory;
import com.xiaoleilu.hutool.db.handler.EntityListHandler;
import com.xiaoleilu.hutool.db.handler.PageResultHandler;
import com.xiaoleilu.hutool.util.CollectionUtil;
import com.xiaoleilu.hutool.util.StrUtil;
import com.xjiye_test.payServer.pojo.PayRecord;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class OrderDao {
    final private SqlRunner runner = SqlRunner.create(DSFactory.get("group_db3"));

    public void addOrderInfo(PayRecord pay) throws SQLException {
        runner.insert(Entity.create("payrecord")
                .set("time",pay.getTime())
                .set("appid",pay.getAppid())
                .set("suppId",pay.getSuppId())
                .set("wechatOrderNo",pay.getWechatOrderNo())
                .set("suppOrderNo",pay.getSuppOrderNo())
                .set("openid",pay.getOpenid())
                .set("paytype",pay.getPaytype())
                .set("paystatus",pay.getPaystatus())
                .set("amount",pay.getAmount())
                .set("wechatRefundNo",pay.getWechatRefundNo())
                .set("suppRefundNo",pay.getSuppRefundNo())
                .set("refundAmount",pay.getRefundAmount())
                .set("fee",pay.getFee())
                .set("feeAmount",pay.getFeeAmount())
        );
    }

    public PageResult<Entity> getDataByCondition(Page page, List<String> keys, List<String> values) throws SQLException {
//        Entity entity=Entity.create("payrecord");
//        for(int i=0;i<keys.size();i++){
//            entity.set(keys.get(i),values.get(i));
//        }
//        return runner.page(entity, page);
        StringBuffer sql=new StringBuffer("SELECT * from payrecord ");
        StringBuffer countSql=new StringBuffer("SELECT count(*) as count from payrecord ");
        if(keys.size()>0){
            sql.append("WHERE ");
        }
        if(keys.size()>0){
            countSql.append("WHERE ");
        }
        for(int i=0;i<keys.size();i++){
            sql.append(keys.get(i)+"? and ");
        }
        for(int i=0;i<keys.size();i++){
            countSql.append(keys.get(i)+"? and ");
        }
        String runSql= StrUtil.sub(sql.toString(),0,-4);
        String runCountSql=StrUtil.sub(countSql.toString(),0,-4);
        runSql+=" limit "+page.getPageNumber()*page.getNumPerPage()+","+(page.getPageNumber()+1)*page.getNumPerPage();
        Object[] objects=new Object[values.size()];
        for(int i=0;i<values.size();i++){
            objects[i]=values.get(i);
        }
        Entity count= runner.query(runCountSql,new EntityListHandler (),objects).get(0);
        return runner.query(runSql,new PageResultHandler(new PageResult(page.getPageNumber(), page.getNumPerPage(), count.getInt("count"))),objects);
    }

}
